const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentValue: '',
    time: '',
    inputUp: false,
    minLength: 3
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    app.request(app.apiDomain + 'index.php?c=step&a=getStep', {
      openid: wx.getStorageSync('openid')
    }, function (res) {
      console.log(res)
      if (res.data.status == 0) {
        that.setData({
          hasOrder: false
        })
      } else {
        that.setData({
          hasOrder: true,
          order_id: res.data.data.id
        })
      }
    })
  },
  inputChange: function (e) {
    let currentValue = e.detail
    this.setData({
      currentValue
    })
    console.log(this.data.currentValue)
  },
  inputFocus(e) {
    console.log(e, '键盘弹起')
    var inputHeight = 0
    if (e.detail.height) {
      inputHeight = e.detail.height
    }
  },
  minFocus: function () {
    var that = this
    that.setData({
      minFocus: true
    })
  },
  complete: function () {
    var that = this
    that.setData({
      minFocus: false
    })
  },
  clearCurrentValue() {
    this.setData({
      currentMin: ''
    })
  },
  setCurrentMin: function (e) {
    var that = this
    let currentMin = e.detail.value
    that.setData({
      currentMin
    })
    if (currentMin.length <= that.data.minLength) {
      // that.triggerEvent('change', currentValue)
      that.setData({
        currentMin
      })
    } else {
      that.complete()
      return
    }
  },
  blurInput:function () {
    let that = this
    that.setData({
      minFocus: false
    })
  },
  // 
  inputBlur() {
    console.log('键盘收起')
  },
  // keyInput: function (e) {
  //   let time = e.detail.value
  //   this.setData({
  //     time
  //   })
  // },
  begin: function () {
    var that = this
    let reg = /^[0-9]{3}$/
    let reg2 = /^[0-9]{1,3}$/
    console.log(reg2.test(that.data.currentMin))
    if (!reg.test(that.data.currentValue)) {
      wx.showToast({
        title: '公里格式填写错误',
        icon: 'none'
      })
      return false
    } else if (!reg2.test(that.data.currentMin)) {
      wx.showToast({
        title: '时间格式填写错误',
        icon: 'none'
      })
      return false 
    }
    if (!that.data.hasOrder) {
      wx.navigateTo({
        url: './run?km=' + (that.data.currentValue / 10) + '&time=' + that.data.currentMin + '&status=0',
      })
    } else {
      wx.navigateTo({
        url: './index',
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    app.request(app.apiDomain + 'index.php?c=step&a=getStep', {
      openid: wx.getStorageSync('openid')
    }, function (res) {
      console.log(res)
      if (res.data.status == 0) {
        that.setData({
          hasOrder: false
        })
      } else {
        that.setData({
          hasOrder: true,
          order_id: res.data.data.id
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})